package com.hejia.alauda.persistence.mybatis;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hejia.alauda.entity.Balance;
import com.hejia.alauda.entity.User;
import com.hejia.alauda.persistence.BaseMapper;

public interface BalanceMapper extends BaseMapper<Balance> {
	
	@Select("select * from `Balance` where id=#{id}")
	@ResultMap("DetailMap")
	Balance findBalanceByUserId(User user);
	
	@Update("UPDATE `balance` b,(SELECT `inviter_id`,SUM(`income`) as `income` FROM `invitation` WHERE `status`=\'CASHED\' AND `type`=2 GROUP BY `inviter_id` ) i SET b.`cash`=i.`income` WHERE b.`id`=i.`inviter_id`")
	int updateCashById(@Param("cash") Double cash,@Param("id")Long id);
	
	@Update("UPDATE `balance` SET `cashed`=IFNULL(`cashed`,0)+#{cashed},`actRed`=IFNULL(`actRed`,0)+#{cashed} WHERE `id`=#{id}")
	int updateCashedById(@Param("cashed") Double cash,@Param("id")Long id);
}
